$(function () {
    var initialLocaleCode = 'zh-cn';
    var newEventFlag = true;

    // 配置fullCalendar
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,today,next',
            center: 'title',
            right: 'month,listMonth'
        },

        // listDayFormat: 'D MMMM YYYY',
        // listDayFormat: false,
        listDayAltFormat: false,

        firstDay:1,

        // defaultDate: '2018-10-12',
        locale: initialLocaleCode,

        showNonCurrentDates: false,

        buttonIcons: false,
        editable: false,
        draggable: false,
        weekNumbers: true,
        navLinks: false, // 禁止通过点击日期到日视图
        selectable: true,
        selectHelper: false,
        eventLimit: true, // allow "more" link when too many events

        eventTextColor: '#000000',


        // 显示每日事件
        // 异步获取月事件，自带起始日期
        events: {
            url: 'ajaxs/events',
            error: function () {
                $('#script-warning').show();
            }
        },

        eventRender: function (event, element, view) {
            // $(element).find(".fc-list-item-title").after('<td class="fc-list-item-title fc-widget-content"><a>' + event.remark + '</a></td>');
            $(element).find(".fc-list-item-title").prepend('<a style="margin-left: 30px; " >' + event.manhour + 'h   </a>').append('<a style="margin-left: 30px; " >' + event.remark + '</a>');
        },


        // 显示每日工时总和
        eventAfterAllRender: function (view) {
            var start = view.start.format('YYYY-MM-DD');
            var end = view.end.format('YYYY-MM-DD');
            $.ajax({
                url: 'ajaxs/count?start=' + start + '&end=' + end,
                success: function (data) {
                    for (cDay = view.start.clone(); cDay.isBefore(view.end); cDay.add(1, 'day')) {
                        var dateNum = cDay.format('YYYY-MM-DD');
                        var dayEl = $('.fc-day[data-date="' + dateNum + '"]');

                        var count = data[dateNum];
                        if (count == null) {
                            count = '0';
                        };

                        var color = 'White';
                        var i = parseInt(count);
                        if (i <= 0) {
                            color = 'White';
                        } else if (i <= 8) {
                            color = 'Green'
                        } else {
                            color = 'Red';
                        }

                        var html = '<span class="event-count" style="color:' + color + '">' +
                            '<i>' +
                            count +
                            '</i>' +
                            '</span>';

                        dayEl.empty();
                        dayEl.append(html);
                    }
                }
            });
        }
    });






});
